<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//

/**
 * Imports a Javascript library
 *
 * @param string $js_src
 */
function importJavascript( $js_src ) {
	Gecko_Template::registerLibrary( $js_src );
}

/**
 * Loads a Javascript library from the library
 * or js repository
 *
 * @param unknown_type $js
 * @param unknown_type $from_lib
 */
function useJavascript( $js, $from_lib = false ) {
	if( $from_lib ) {
		Gecko_Template::useHelper( $js );
	} else {
		$cfg = Zend_Registry::get('config');
		$js = $cfg->pageRootURL . "/web/js/" . $js;
		Gecko_Template::registerLibrary(  $js );
	}
}

/**
 * Creates a hover table
 *
 * @param string $table_id
 * @param string $hover_color
 * @return string
 */
function hover_table( $table_id, $hover_color ) {
	useJavascript( "GeckoHoverTable.js", true );

	$code = "try {\n";
	$code.= "var hover_$table_id = new GeckoHoverTable(\"$table_id\", \"$hover_color\");\n";
	$code.= "} catch( e ) { alert( e ); }\n";

	return _javascript_tag( $code );
}

/**
 * Creates a Editable Div that updates at Remote URL
 * Response must be OK|response, else it will mark
 * error
 *
 * @param string $div_id
 * @param string $content
 * @param string $remote_url
 * @return string
 */
function editable_div( $div_id, $content, $remote_url ) {
	useJavascript('prototype.js', true);
	useJavascript('GeckoEditableDiv.js', true);

	$code = "var " . $div_id ."_editable = new GeckoEditableDiv( '$div_id', '$remote_url' );\n";

	$code = _javascript_tag($code);

	return _construct_tag("div", array( 'id' => $div_id ), $content) . $code;
}

/**
 * Inner function to create a Javascript tag
 *
 * @param string $code
 * @return string
 */
function _javascript_tag( $code ) {
	$return = "<script type=\"text/javascript\">\n";
	$return.= "//<![CDATA[\n";
	$return.= $code;
	$return.= "\n//]]>\n";
	$return.= "</script>";

	return $return;
}
?>